<?php
/**
 * Disorder model class file.
 *
 * @package   models
 * @since     1.0
 * @filesource
 */

/**
 * This is the model class for table "{{disorder}}".
 * This is a DAO class which is used by DisorderController or other controllers
 * to operate data in database or return data in a specified format.
 * 
 * The followings are the available columns in table '{{disorder}}':
 * @property integer $id
 * @property string $name
 * @property string $abbr
 * @property integer $active
 *
 * The followings are the available model relations:
 * @property PatientSignal[] $patientSignals - an array of PatientSignal objects for a kind of disorder.
 */
class Disorder extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Disorder the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * Returns the table name related to this model class.
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{disorder}}';
	}

	/**
	 * Returns the validation rules.
	 * The rules will be used when requesting validation by call self.validate().
	 * Many class method will call self.validate(), eg: CActiveRecord.save().
	 * @return array validation rules for model attributes. 
	 */
	public function rules()
	{
		return array(
			//required attributes
			array('name, abbr, active', 'required'),
			array('active', 'numerical', 'integerOnly'=>true),
			array('name, abbr', 'length', 'max'=>255),
			// The following rule is used by search().
			array('name, abbr, active', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * Returns the relationship between this Model class and other Model class
	 * based on foreign key constraint between corresponding tables.
	 * The relationships involved in this application involves HAS_MANY, HAS_ONE, BELONGS_TO,
	 * please refer to Yii document for details.
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			// one disorder could be related to many patient signals.
			'patientSignals' => array(self::HAS_MANY, 'PatientSignal', 'disorderId'),
		);
	}

	/**
	 * Returns the declaration of named scopes.
	 * @see CActiveRecord::scopes()
	 */
	public function scopes(){
		return array(
			'active'=>array(
		            'condition'=>'active=1',
			),
		);
	}

	/**
	 * Returns the labels for each column variables. 
	 * This will be used to render label on page.
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'abbr' => 'Abbr.',
			'active' => 'Active',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions 
	 * declared in this function.
	 * @return CActiveDataProvider the data provider that can return the models 
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		$criteria=new CDbCriteria;
		$criteria->compare('name',$this->name,true);
		$criteria->compare('abbr',$this->abbr,true);
		$criteria->compare('active',$this->active);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * Get all disorders and return the information in array.
	 * @return array. Array[id]=name
	 */
	public static function getAllDisorders(){
		$disorder=self::model()->findAll(array(
			'select'=>'id,name',
		));
		return CHtml::listData($disorder, 'id', 'name');
	}
	
	/**
	 * Get all validate disorders and return the information in array.
	 * @return array. Array[id]=name
	 */
	public static function getAllActiveDisorders(){
		$disorder=self::model()->findAll(array(
			'select'=>'id,name',
			'condition'=>'active=1',
		));
		return CHtml::listData($disorder, 'id', 'name');
	}
	
	/**
	 * Get string of abbrivation of a disorder by a given disorder id.
	 * @param int $id integer code for a disorder.
	 * @return string. Then abbrivation of a disorder.
	 */
	public static function getDisorderAbbr($id){
		$disorder=self::model()->findByPk($id);
		return $disorder->abbr;
	}
}